package com.adcc.swingui.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.adcc.swingui.bean.MenuRightBean;
import com.adcc.swingui.bean.MenuRightComponentBean;

public class MenuRightBarConf {
	private static Logger logger = Logger.getLogger(MenuRightBarConf.class);
	private static Map<String, MenuRightBean> menuRightConf = new HashMap<String, MenuRightBean>();

	public static void initConf(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					MenuRightBean bean = new MenuRightBean();
					List<MenuRightComponentBean> componentList = new ArrayList<MenuRightComponentBean>();
					Element menuRightBar = (Element) i.next();
					bean.setName(menuRightBar.attributeValue("name"));
					bean.setClassUrl(menuRightBar.attributeValue("classurl"));
					for (Iterator j = menuRightBar.elementIterator(); j
							.hasNext();) {
						Element component = (Element) j.next();
						MenuRightComponentBean componentBean = new MenuRightComponentBean();
						componentBean.setName(component.attributeValue("name"));
						componentBean.setI18nName(component
								.attributeValue("i18nName"));
						componentBean.setIcon(component.attributeValue("icon"));
						componentBean.setShortcutkey(component
								.attributeValue("shortcutkey"));
						componentBean.setMethodname(component
								.attributeValue("methodname"));
						componentBean.setMethodClassName(component
								.attributeValue("classurl"));
						componentBean.setMenuClass(component
								.attributeValue("menuClass"));
						componentBean.setPrivilegestr(component
								.attributeValue("privilegestr"));
						componentList.add(componentBean);
					}
					bean.setMenuRightList(componentList);
					menuRightConf.put(bean.getName(), bean);
				}

			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	public static void initConf(String path, List<String> privilege) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					MenuRightBean bean = new MenuRightBean();
					List<MenuRightComponentBean> componentList = new ArrayList<MenuRightComponentBean>();
					Element menuRightBar = (Element) i.next();
					bean.setName(menuRightBar.attributeValue("name"));
					bean.setClassUrl(menuRightBar.attributeValue("classurl"));
					for (Iterator j = menuRightBar.elementIterator(); j
							.hasNext();) {
						Element component = (Element) j.next();
						MenuRightComponentBean componentBean = new MenuRightComponentBean();
						componentBean.setName(component.attributeValue("name"));
						componentBean.setI18nName(component
								.attributeValue("i18nName"));
						componentBean.setIcon(component.attributeValue("icon"));
						componentBean.setShortcutkey(component
								.attributeValue("shortcutkey"));
						componentBean.setMethodname(component
								.attributeValue("methodname"));
						componentBean.setMethodClassName(component
								.attributeValue("classurl"));
						componentBean.setMenuClass(component
								.attributeValue("menuClass"));
						componentBean.setPrivilegestr(component
								.attributeValue("privilegestr"));
						if (haveAuthority(componentBean, privilege)) {
							componentList.add(componentBean);
						}
					}
					bean.setMenuRightList(componentList);
					menuRightConf.put(bean.getName(), bean);
				}

			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	private static boolean haveAuthority(MenuRightComponentBean cb,
			List<String> privilege) {
		return cb.getPrivilegestr() == null
				|| cb.getPrivilegestr().trim().equals("")
				|| privilege.contains(cb.getPrivilegestr());
	}

	public static Map<String, MenuRightBean> getMenuRightConf() {
		return menuRightConf;
	}

	public static List<MenuRightBean> getMenuRight(String path,
			List<String> privilege) {
		List<MenuRightBean> list = new ArrayList<MenuRightBean>();
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					MenuRightBean bean = new MenuRightBean();
					List<MenuRightComponentBean> componentList = new ArrayList<MenuRightComponentBean>();
					Element menuRightBar = (Element) i.next();
					bean.setName(menuRightBar.attributeValue("name"));
					bean.setClassUrl(menuRightBar.attributeValue("classurl"));
					for (Iterator j = menuRightBar.elementIterator(); j
							.hasNext();) {
						Element component = (Element) j.next();
						MenuRightComponentBean componentBean = new MenuRightComponentBean();
						componentBean.setName(component.attributeValue("name"));
						componentBean.setI18nName(component
								.attributeValue("i18nName"));
						componentBean.setIcon(component.attributeValue("icon"));
						componentBean.setShortcutkey(component
								.attributeValue("shortcutkey"));
						componentBean.setMethodname(component
								.attributeValue("methodname"));
						componentBean.setMethodClassName(component
								.attributeValue("classurl"));
						componentBean.setMenuClass(component
								.attributeValue("menuClass"));
						componentBean.setPrivilegestr(component
								.attributeValue("privilegestr"));
						if (haveAuthority(componentBean, privilege)) {
							componentList.add(componentBean);
						}
					}
					bean.setMenuRightList(componentList);
					list.add(bean);
				}
			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
		return list;
	}

	public static void main(String[] args) {
		initConf("conf/default/menu_right.xml");
		Set<String> keySet = menuRightConf.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String key = it.next();
			MenuRightBean value = menuRightConf.get(key);
			System.out.println(value.getClassUrl());
			List<MenuRightComponentBean> a = new ArrayList<MenuRightComponentBean>();
			a = value.getMenuRightList();
			if (a != null && a.size() != 0) {
				for (int i = 0; i < a.size(); i++) {
					MenuRightComponentBean comp = new MenuRightComponentBean();
					comp = a.get(i);
					System.out.println("componentName:" + comp.getName());
					System.out.println("componentClassurl:"
							+ comp.getMethodClassName());
					System.out.println("menuClass:" + comp.getMenuClass());
					System.out.println("-----------------------" + i);

				}
			}
		}

	}
}
